package com.lee.mysql.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lee.mysql.dao.MenuDao;
import com.lee.mysql.dao.RoleMenuDao;
import com.lee.mysql.pojo.Menu;
import com.lee.mysql.util.IdGenUtil;
import com.lee.mysql.vo.MenuVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 操作菜单
 */
@Slf4j
@Service
@Transactional
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RoleMenuDao roleMenuDao;



    /** 新增操作菜单 */
    public int addRecord(Menu record){
        record.setId(IdGenUtil.flakeId());
        if (StringUtils.isBlank(record.getParentId())){
            record.setParentId("0");
        }

        int lev;
        Menu parent = menuDao.queryOneById(record.getParentId());
        if (parent == null || "0".equals(parent.getId())){
            lev = 1;
        }else {
            lev = parent.getLevel() + 1;
        }
        record.setLevel(lev);

        return menuDao.insertOne(record);
    }

    /** 根据ID修改记录 */
    public int updateOneById(Menu record){
        int lev;
        Menu parent = menuDao.queryOneById(record.getParentId());
        if (parent == null || "0".equals(parent.getId())){
            lev = 1;
        }else {
            lev = parent.getLevel() + 1;
        }
        record.setLevel(lev);

        return menuDao.updateById(record);
    }

    /** 根据ID批量删除记录 */
    public int deleteByIds(List<String> idList){
        roleMenuDao.deleteByMenuIds(idList);
        return menuDao.deleteByIds(idList);
    }

    /** 根据ID查询记录 */
    public Menu queryOneById(String id){
        return menuDao.queryOneById(id);
    }


    /** 查询分页数据 */
    public PageInfo<Menu> queryPaging(Menu record, Integer page, Integer size){

        PageHelper.startPage(page, size);
        List<Menu> list = menuDao.getList(record);

        return new PageInfo<>(list);
    }


    /** 查询所有菜单 */
    public List<Menu> queryAllMenu(String type){

        List<Menu> list = menuDao.queryAllMenu(type);

        return list;
    }


    /** 查询所有菜单及子菜单 */
    public List<MenuVo> queryMenuAndChild(String type,String roleId){
        List<String> roleMenuIds = new ArrayList<>();
        if (StringUtils.isNotBlank(roleId)){
            // 解决layui回调显示问题
            roleMenuIds = menuDao.getRoleMenuIds(roleId);
//            List<String> ids = menuDao.getRoleMenuIds(roleId);
//            List<String> pIds = menuDao.getRoleMenuParIds(roleId);
//            for (String id : ids) {
//                if (!pIds.contains(id)){
//                    roleMenuIds.add(id);
//                }
//            }
        }

        // 查询所有顶级菜单
        List<MenuVo> list = menuDao.queryParentMenu();
        if (list.size() > 0){
            list = queryChild(list,type,roleMenuIds);
        }

        return list;
    }

    public List<MenuVo> queryChild(List<MenuVo> list,String type,List<String> roleMenuIds){
        list.forEach(item->{
            if (roleMenuIds.size() > 0){
                if (roleMenuIds.contains(item.getId())){
                    item.setChecked(true);
                }
            }

            if ("0".equals(item.getMenuType())){
                List<MenuVo> child = menuDao.queryChildMenu(item.getId(),type);
                if (child.size() > 0){
                    child = queryChild(child,type,roleMenuIds);
                    item.setChildren(child);
                }
            }
        });

        return list;
    }




}
